package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.BulkGroupDAO;
import com.klikmee.sms.chat.web.dao.BulkGroupItemDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.BulkGroup;
import com.klikmee.sms.services.chat.model.BulkGroupItem;

public class BulkGroupForm extends ClientApplication {

	private CollectionDataSource dsBulkGroups;
	private CollectionDataSource dsBulkGroupItems;
	private ArrayList<String> bcBulkGroups;
	private ArrayList<String> bcBulkGroupItems;
	private BulkGroupDAO groupDAO;
	private BulkGroupItemDAO groupItemDAO;
	private List<BulkGroup> groups;
	private List<BulkGroupItem> groupItem;
	private BulkGroupDetailsForm details;
	private BulkGroupAddItemForm groupItemForm;

	private boolean itemAdded;

	public void load() throws Exception {
		super.load();

		this.dsBulkGroups = new CollectionDataSource("dsBulkGroups");
		this.getDataSources().add(this.dsBulkGroups);
		this.bcBulkGroups = new ArrayList<String>();
		this.bcBulkGroups.add("GroupName");
		this.bcBulkGroups.add("GroupDescription");
		this.bcBulkGroups.add("Active");

		this.dsBulkGroupItems = new CollectionDataSource("dsBulkGroupItems");
		this.getDataSources().add(this.dsBulkGroupItems);
		this.bcBulkGroupItems = new ArrayList<String>();
		this.bcBulkGroupItems.add("PhoneNumber");
		this.bcBulkGroupItems.add("Note");

		this.triggers.add(new Trigger(this, "btnEditGroupClick"));
		this.triggers.add(new Trigger(this, "btnAddGroupClick"));
		this.triggers.add(new Trigger(this, "btnDeleteGroupClick"));
		this.triggers.add(new Trigger(this, "btnAddIntoGroupClick"));
		this.triggers.add(new Trigger(this, "btnDeleteFromGroupClick"));
		this.triggers.add(new Trigger(this, "gvBulkGroupsCellClick"));
		this.triggers.add(new Trigger(this, "gvBulkGroupsCellDoubleClick"));
		this.triggers.add(new Trigger(this, "gvBulkGroupItemsCellClick"));
		this.triggers.add(new Trigger(this, "gvBulkGroupItemsCellDoubleClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();

		ChangeControlCollapsible(
				"AppBulkGroup_borderLayout1_borderLayout1PanelRight", true);

		bindBulkGroupsList();
		bindItemForGroup();
	}

	public void btnEditGroupClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		if (this.dsBulkGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			details = createChildApplication(BulkGroupDetailsForm.class);
			details.setGroup((BulkGroup) this.dsBulkGroups.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this,
					"afterGroupSave");
			details.setEditGroup(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddGroupClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		details = createChildApplication(BulkGroupDetailsForm.class);
		details.setGroup(new BulkGroup());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterGroupSave");
		details.setEditGroup(false);
		this.showChildApp(details);
		details.start();
	}

	public void btnDeleteGroupClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		if (this.dsBulkGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			BulkGroup group = (BulkGroup) this.dsBulkGroups.getSelectedItem();
			group.setActive(false);
			groupDAO.edit(group);
			bindBulkGroupsList();
		}
	}

	public void gvBulkGroupsCellClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		bindItemForGroup();
		ChangeControlCollapsible(
				"AppBulkGroup_borderLayout1_borderLayout1PanelRight", false);
	}

	public void gvBulkGroupsCellDoubleClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		details = createChildApplication(BulkGroupDetailsForm.class);
		details.setGroup((BulkGroup) this.dsBulkGroups.getSelectedItem());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterGroupSave");
		details.setEditGroup(true);
		this.showChildApp(details);
		details.start();
	}

	public void btnAddIntoGroupClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		if (this.dsBulkGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			BulkGroupItem newItem = new BulkGroupItem();
			BulkGroup selectedGroup = (BulkGroup) this.dsBulkGroups
					.getSelectedItem();
			newItem.setBulkGroup(selectedGroup);

			groupItemForm = createChildApplication(BulkGroupAddItemForm.class);
			groupItemForm.triggers.get("btnSaveClick").addOnFire(this,
					"afterItemAdd");
			groupItemForm.setGroup(newItem);
			this.showChildApp(groupItemForm);
			groupItemForm.start();
		}
	}

	public void btnDeleteFromGroupClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {
		if (!groupItem.isEmpty()) {
			groupItemDAO.delete(groupItem.get(0));
			bindItemForGroup();
		} else {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati broj!");
			return;
		}
	}

	public void gvBulkGroupItemsCellClick() throws InstantiationException,
			IllegalAccessException, JSONException, Exception {

		groupItemDAO = SpringBeansLookup.getBean(BulkGroupItemDAO.class);
		BulkGroup selectedGroup = (BulkGroup) this.dsBulkGroups
				.getSelectedItem();
		Integer itemId = ((BulkGroupItem) this.dsBulkGroupItems
				.getSelectedItem()).getId();
		BulkGroupItem check = (BulkGroupItem) groupItemDAO
				.findItemByBulkGroupAndItem(selectedGroup, itemId);

		groupItem.add(check);
	}

	public void gvBulkGroupItemsCellDoubleClick()
			throws InstantiationException, IllegalAccessException,
			JSONException, Exception {
	}

	private void bindBulkGroupsList() throws SecurityException,
			ClassNotFoundException, NoSuchFieldException,
			IllegalAccessException, JSONException, NoSuchMethodException,
			InvocationTargetException {
		groupDAO = SpringBeansLookup.getBean(BulkGroupDAO.class);
		groups = groupDAO.selectAll();
		this.dsBulkGroups.setSource(groups);
		this.dsBulkGroups.bind(bcBulkGroups);
	}

	private void bindItemForGroup() throws SecurityException,
			ClassNotFoundException, NoSuchFieldException,
			IllegalAccessException, JSONException, NoSuchMethodException,
			InvocationTargetException {
		groupItemDAO = SpringBeansLookup.getBean(BulkGroupItemDAO.class);
		setGroupItem(groupItemDAO
				.findBulkGroupItem((BulkGroup) this.dsBulkGroups
						.getSelectedItem()));
		this.dsBulkGroupItems.setSource(getGroupItem());
		this.dsBulkGroupItems.bind(bcBulkGroupItems);

		setGroupItem(new ArrayList<BulkGroupItem>());
	}

	public void afterGroupSave() throws SecurityException,
			ClassNotFoundException, NoSuchFieldException,
			IllegalAccessException, JSONException, NoSuchMethodException,
			InvocationTargetException {
		bindBulkGroupsList();
	}

	public void afterItemAdd() throws SecurityException,
			ClassNotFoundException, NoSuchFieldException,
			IllegalAccessException, JSONException, NoSuchMethodException,
			InvocationTargetException {

		if (groupItemForm.getItemAdded()) {
			BulkGroupItem bulkGroupAddItem = (BulkGroupItem) ((ObjectDataSource) (groupItemForm
					.getDataSources().get("dsBulkGroupItem"))).getSource();

			BulkGroup selectedGroup = (BulkGroup) this.dsBulkGroups
					.getSelectedItem();
			bindItemForGroup();
			MessageBox.Show(this, "Obavestenje", String.format(
					"Broj %s je dodat u %s grupu!",
					bulkGroupAddItem.getPhoneNumber(),
					selectedGroup.getGroupName()));

			groupItemForm.hide();
		}
	}

	public void setGroupItem(List<BulkGroupItem> groupItem) {
		this.groupItem = groupItem;
	}

	public List<BulkGroupItem> getGroupItem() {
		return groupItem;
	}

	public void setItemAdded(boolean itemAdded) {
		this.itemAdded = itemAdded;
	}

	public boolean getItemAdded() {
		return itemAdded;
	}
}
